from flask import Flask
from flask import request
from flask import make_response
from flask_cors import CORS
import numpy as np
import json
from urllib.parse import unquote
from data_process import PeopleInfo

app = Flask(__name__)
CORS(app, resources=r'/*')
people_info = PeopleInfo()
print("finish init")
@app.route("/", methods=['GET', 'POST'])
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return "login GET"
    else:
        json_ori = request.get_json()
        if json_ori["type"] == "people_info":
            name = json_ori["query_name"]
            name = unquote(name)
            return people_info.get_people_info(name=name)
        elif json_ori["type"] == "statistics":
            feature = json_ori["feature"]
            # year = json_ori["year"]
            return people_info.get_static(feature=feature, year=False)
        elif json_ori["type"] == "race":
            feature = json_ori["feature"]
            return json.dumps(people_info.get_static(feature=feature, year=True), ensure_ascii=False, indent=4)
        # return {
        #     "username": json_ori["username"],
        #     "topic": "test",
        #     "data": list(np.around(np.random.uniform(0, 1, (3)), decimals=3)),
        # }